package com.micro.common.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.util.StringUtils;

import java.io.Serializable;

/**
 * @author pan.shun
 * @since 2022/3/14 16:59
 */
@SuppressWarnings("unchecked")
public class R<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <E> R<E> success(E data) {
        return new R(200, (String) null, data);
    }

    public static <E> R<E> success(E data, String msg) {
        return new R(200, msg, data);
    }

    public static <E> R<E> success() {
        return new R(200, (String) null, (Object) null);
    }


    public static R error(String msg) {
        R result = new R();
        result.setCode(500);
        result.setMsg(msg);
        return result;
    }

    public static R error(Integer code, String msg) {
        R result = new R();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static R error() {
        R result = new R();
        result.setCode(500);
        result.setMsg("未知异常，请联系管理员");
        return result;
    }

    public static R forbidden() {
        R result = new R();
        result.setCode(403);
        result.setMsg("权限不足，请联系管理员");
        return result;
    }

    public static R loginError(String msg) {
        R result = new R();
        result.setCode(401);
        if (StringUtils.isEmpty(msg)) {
            result.setMsg("登录已失效,请重新登录");
        } else {
            result.setMsg(msg);
        }
        return result;
    }

    public R<T> put(T data) {
        this.data = data;
        return this;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.code == 200;
    }

    @JsonIgnore
    public boolean isError() {
        return this.code != 200;
    }

    public Integer getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }

    public R<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public R<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public R<T> setData(T data) {
        this.data = data;
        return this;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof R)) {
            return false;
        } else {
            R<?> other = (R) o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47:
                {
                    Object this$code = this.getCode();
                    Object other$code = other.getCode();
                    if (this$code == null) {
                        if (other$code == null) {
                            break label47;
                        }
                    } else if (this$code.equals(other$code)) {
                        break label47;
                    }

                    return false;
                }

                Object this$msg = this.getMsg();
                Object other$msg = other.getMsg();
                if (this$msg == null) {
                    if (other$msg != null) {
                        return false;
                    }
                } else if (!this$msg.equals(other$msg)) {
                    return false;
                }

                Object this$data = this.getData();
                Object other$data = other.getData();
                if (this$data == null) {
                    if (other$data != null) {
                        return false;
                    }
                } else if (!this$data.equals(other$data)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof R;
    }

    public String toString() {
        return "R(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ")";
    }

    public R() {
    }
}